package com.junmoo.www.auth.module.system.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.junmoo.www.entity.SysMenu;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    @Select("<script>"
            + "select m.* from sys_menu m,sys_role_menu  rm where m.menu_id =rm.menu_id and rm.role_id =#{roleId}"
            + "</script>")
    @Results(id="resultMap",value = {
            @Result(column = "menu_id",property ="id",id = true )
    })
    public List<SysMenu> findByRoleId(@Param("roleId") Long roleId);

    @Select("<script>"
            + "select distinct m.* from sys_menu m ,sys_role_menu rm,sys_user_role ur " +
            " where ur.role_id =rm.role_id and rm.menu_id = m.menu_id and ur.user_id = #{userId}"
            + "</script>")
    @ResultMap("resultMap")
    public List<SysMenu> findByUserId(@Param("userId") Long userId);

    @Select("<script>"
            + "WITH RECURSIVE temp AS (\n" +
            "    SELECT * FROM sys_menu  WHERE menu_id =#{menuId} \n" +
            "    UNION ALL \n" +
            "    SELECT m.* FROM sys_menu m,temp t WHERE t.parent_id = m.menu_id\n" +
            " )select * from temp"
            + "</script>")
    @ResultMap("resultMap")
    List<SysMenu> findByTreeByUp(@Param("menuId") Long menuId);

    @Select("<script>"
            + "WITH RECURSIVE temp AS (\n" +
            "        SELECT * FROM sys_menu  WHERE menu_id =#{menuId} \n" +
            "        UNION ALL\n" +
            "        SELECT m.* FROM sys_menu m,temp t WHERE t.menu_id = m.parent_id\n" +
            " )select * from temp"
            + "</script>")
    @ResultMap("resultMap")
    List<SysMenu> findByTreeByDown(@Param("menuId") Long menuId);




////--  父 求 子
//WITH RECURSIVE temp AS (
//        SELECT * FROM sys_menu m WHERE m.menu_id =1200
//        UNION ALL
//        SELECT r.* FROM sys_menu m,temp t WHERE t.menu_id = r.parent_id
//)select * from temp
//
//
////-- 子 求 父
//WITH RECURSIVE temp AS (
//        SELECT * FROM sys_resource r WHERE r.resource_id =1210
//        UNION ALL
//        SELECT r.* FROM sys_resource r,temp t WHERE t.parent_id = r.resource_id
//)select * from temp





}
